package com.huawei.controller;

import com.alibaba.fastjson.JSON;
import com.huawei.entity.Sytuijian;
import com.huawei.service.SytuijianService;
import com.huawei.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName User_siteController
 * @Description TODO
 * @Author 闫修奇
 * @Date 2021/11/27 17:20
 */

@Controller
@RequestMapping("/Sytuijian")

public class SytuijianController {
    @Resource
    SytuijianService sytuijianServiceImpl;

    @RequestMapping(value = "/chaxun", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String chaxun(
            @RequestParam(value = "sp_name", required = false) String sp_name,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam("page") Integer page,
            @RequestParam("limit") Integer limit) {
         Map<String, Object> pars = new HashMap<>();
        pars.put("offset", (page-1)*limit);
        pars.put("limit", limit);
        pars.put("sp_name", sp_name);
        pars.put("status", status);
        List<Sytuijian> list = sytuijianServiceImpl.select(pars);
        int count = sytuijianServiceImpl.count(pars);
        Result result = new Result(0, "查询成功", count, list);
        return JSON.toJSONString(result);

    }

    @RequestMapping(value = "/add", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String add(
            Sytuijian  sytuijian
    ) {

        int n = sytuijianServiceImpl.add(sytuijian);
        if (n == 1) {
            return "添加成功";
        } else {
            return "添加失败";
        }

    }



    @RequestMapping(value = "/update", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String update(
        Sytuijian sytuijian
    ) {
        int n = sytuijianServiceImpl.update(sytuijian);
        if (n == 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }

    }



    @RequestMapping(value = "/update1", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String update1(
            @RequestParam("id") Integer id,
            @RequestParam("status") String  status
      ) {

        int n = sytuijianServiceImpl.update1(id,status);
        if (n == 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }

    }

    @RequestMapping(value = "delete", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String delect(  @RequestParam ("id")  Integer id) {

        int n = sytuijianServiceImpl.delete(id);
        if (n == 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }

    }


}
